package top.tangyh.lamp.care.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.tangyh.lamp.care.entity.BDevBrandType;
import top.tangyh.lamp.care.entity.BPgSrx;
import top.tangyh.lamp.care.mapper.BDevBrandTypeMapper;
import top.tangyh.lamp.care.mapper.BEquipmentBrandMapper;
import top.tangyh.lamp.care.service.BEquipmentBrandService;
import top.tangyh.basic.base.service.impl.SuperServiceImpl;
import top.tangyh.lamp.care.manager.BEquipmentBrandManager;
import top.tangyh.lamp.care.entity.BEquipmentBrand;
import top.tangyh.lamp.care.vo.query.EquipmentBrandSearchVO;
import top.tangyh.lamp.care.vo.result.EquipmentBrandDto;
import top.tangyh.lamp.care.vo.result.PageOutputDTO;
import top.tangyh.lamp.care.vo.save.BEquipmentBrandSaveVO;
import top.tangyh.lamp.care.vo.update.BEquipmentBrandUpdateVO;
import top.tangyh.lamp.care.vo.result.BEquipmentBrandResultVO;
import top.tangyh.lamp.care.vo.query.BEquipmentBrandPageQuery;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 设备品牌表
 * </p>
 *
 * @author zqb
 * @date 2024-07-15 17:30:58
 * @create [2024-07-15 17:30:58] [zqb] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
public class BEquipmentBrandServiceImpl extends SuperServiceImpl<BEquipmentBrandManager, Long, BEquipmentBrand> implements BEquipmentBrandService {

    private  final BEquipmentBrandMapper bEquipmentBrandMapper;
    private final BDevBrandTypeMapper bDevBrandTypeMapper;
    @Override
    public PageOutputDTO<EquipmentBrandDto> queryPage(EquipmentBrandSearchVO query) {
        PageHelper.startPage(query.getPage(),query.getSize());
        QueryWrapper<BEquipmentBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted",0);
        queryWrapper.like(StringUtils.isNotBlank(query.getBrandCode()),"brand_code", query.getBrandCode());
        queryWrapper.like(StringUtils.isNotBlank(query.getBrandName()),"brand_name",query.getBrandName());
        List<BEquipmentBrand> list = bEquipmentBrandMapper.selectList(queryWrapper );

        List<EquipmentBrandDto> listDto =new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            BEquipmentBrand equipmentBrandPO=list.get(i);
            EquipmentBrandDto dto= BeanUtil.toBean(equipmentBrandPO, EquipmentBrandDto.class);
            //ConvertUtil.convert(equipmentBrandPO,EquipmentBrandDto.class);

            QueryWrapper<BDevBrandType> queryWrapper_brandType = new QueryWrapper<>();
            queryWrapper_brandType.eq("brand_id",dto.getId());
            queryWrapper_brandType.eq("is_deleted",0);
            List<BDevBrandType> list_brandType = bDevBrandTypeMapper.selectList(queryWrapper_brandType );
            dto.setListBrandType(list_brandType);
            dto.setListBrandTypeSize(list_brandType.size());
            listDto.add(dto);
        }

        PageInfo pageInfo = new PageInfo<>(list);


        return new PageOutputDTO<>(pageInfo.getTotal(), (long) pageInfo.getPages(), (long) pageInfo.getPageNum(), listDto);
    }

    @Override
    public int countOfEquipmentBrandCode(int saveOrUpdate,String brandCode,Long id) {
        QueryWrapper<BEquipmentBrand> queryWrapper=new QueryWrapper();
        // QueryWrapper<Employee> queryWrapper2=Wrappers.<Employee>query();
        if(saveOrUpdate==0){
            queryWrapper.eq("brand_code",brandCode);
            queryWrapper.eq("is_deleted",0);
        }else{
            queryWrapper.eq("is_deleted",0);
            queryWrapper.eq("brand_code",brandCode);
            queryWrapper.ne("id",id);
        }


        Integer count = bEquipmentBrandMapper.selectCount(queryWrapper).intValue();
        return count;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long saveEquipmentBrand(BEquipmentBrand equipmentBrandPO) {
        bEquipmentBrandMapper.insert(equipmentBrandPO);
        return equipmentBrandPO.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateList(List<BEquipmentBrand> list) {
        bEquipmentBrandMapper.updateById(list);
        return true;
    }
}


